package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPackingDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPaymentDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicPaymentService;

@Tag(name = "管理后台 - 付款方式")
@RestController
@RequestMapping("/contract/basic-payment")
@Validated
public class BasicPaymentController {

    @Resource
    private BasicPaymentService basicPaymentService;

    @PostMapping("/create")
    @Operation(summary = "创建付款方式")
    @PreAuthorize("@ss.hasPermission('contract:basic-payment:create')")
    public CommonResult<Long> createBasicPayment(@Valid @RequestBody BasicPaymentSaveReqVO createReqVO) {
        return success(basicPaymentService.createBasicPayment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新付款方式")
    @PreAuthorize("@ss.hasPermission('contract:basic-payment:update')")
    public CommonResult<Boolean> updateBasicPayment(@Valid @RequestBody BasicPaymentSaveReqVO updateReqVO) {
        basicPaymentService.updateBasicPayment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除付款方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-payment:delete')")
    public CommonResult<Boolean> deleteBasicPayment(@RequestParam("id") Long id) {
        basicPaymentService.deleteBasicPayment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得付款方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-payment:query')")
    public CommonResult<BasicPaymentRespVO> getBasicPayment(@RequestParam("id") Long id) {
        BasicPaymentDO basicPayment = basicPaymentService.getBasicPayment(id);
        return success(BeanUtils.toBean(basicPayment, BasicPaymentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得付款方式分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-payment:query')")
    public CommonResult<PageResult<BasicPaymentRespVO>> getBasicPaymentPage(@Valid BasicPaymentPageReqVO pageReqVO) {
        PageResult<BasicPaymentDO> pageResult = basicPaymentService.getBasicPaymentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicPaymentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出付款方式 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-payment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicPaymentExcel(@Valid BasicPaymentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicPaymentDO> list = basicPaymentService.getBasicPaymentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "付款方式.xls", "数据", BasicPaymentRespVO.class,
                        BeanUtils.toBean(list, BasicPaymentRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得付款方式列表")
    public CommonResult<List<BasicPaymentDO>> getBasicPaymentList(@Valid BasicPaymentDO basicPaymentDO) {
        List<BasicPaymentDO> listResult = basicPaymentService.list(Wrappers.lambdaQuery(basicPaymentDO));
        return success(listResult);
    }

}